/*
 * \brief   Pistachio utilities
 * \author  Christian Helmuth
 * \date    2006-04-11
 */

/*
 * Copyright (C) 2006-2009 Christian Helmuth
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _CORE__INCLUDE__UTIL_H_
#define _CORE__INCLUDE__UTIL_H_

#include <base/stdint.h>
#include <base/printf.h>
#include <cpu/touch.h>
#include <pistachio/kip.h>

namespace Pistachio
{
#include <l4/types.h>
#include <l4/ipc.h>
#include <l4/kdebug.h>

	inline void log_event(const char *s) { }
	inline void log_event(const char *s, unsigned v1, unsigned v2, unsigned v3) { }

	inline void panic(const char *s)
	{
		PDBG("Panic: %s", s);
		L4_KDB_Enter("> panic <");
	}

	inline void assert(const char *s, bool val)
	{
	  if (!val) {
	    PERR("Assertion failed: %s", s);
	    L4_KDB_Enter("Assertion failed.");
	  }
	}

	inline void touch_ro(const void *addr, unsigned size)
	{
		const char *bptr, *eptr;
		L4_Word_t mask = get_page_mask();
		L4_Word_t psize = get_page_size();

		bptr = (const char *)(((unsigned)addr) & mask);
		eptr = (const char *)(((unsigned)addr + size - 1) & mask);
		for ( ; bptr <= eptr; bptr += psize)
			Genode::touch_read(bptr);
	}

	inline void touch_rw(const void *addr, unsigned size)
	{
		const char *bptr, *eptr;
		L4_Word_t mask = get_page_mask();
		L4_Word_t psize = get_page_size();

		bptr = (const char *)(((unsigned)addr) & mask);
		eptr = (const char *)(((unsigned)addr + size - 1) & mask);
		for(; bptr <= eptr; bptr += psize)
			Genode::touch_read_write(bptr);
	}

	inline Genode::addr_t trunc_page(Genode::addr_t page)
	{
	  return page & get_page_mask();
	}

	inline Genode::addr_t round_page(Genode::addr_t page)
	{
	  return trunc_page(page + get_page_size() - 1);
	}

	inline void sleep_forever()
	{
	  for (;;)
	    L4_Sleep(L4_Never);
	}
}

#endif /* _CORE__INCLUDE__UTIL_H_ */
